package com.concurrent.sample.business.event.manager.impl;

import com.concurrent.sample.business.common.enums.EventTypeEnum;
import com.concurrent.sample.business.common.enums.SideEnum;
import com.concurrent.sample.business.event.param.trade.CreateTradeEventParamParam;
import com.concurrent.sample.business.event.param.trade.TradeExecReportParamParam;
import com.concurrent.sample.business.event.service.IEventService;
import com.concurrent.sample.business.loan.service.ILoanPositionService;
import com.concurrent.sample.business.sys.service.CashFlowService;
import com.concurrent.sample.business.trade.service.TradeOrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
@Slf4j
public class TradeEventShortOpenServiceImpl extends AbstractTradeEventService {

  private final IEventService eventService;

  private final TradeOrderService orderService;

  private final ILoanPositionService positionService;

  private final CashFlowService cashFlowService;

  @Override
  public EventTypeEnum getEventType() {
    return EventTypeEnum.TRADE_SHORT;
  }

  @Override
  public SideEnum getSide() {
    return SideEnum.SELL;
  }

  @Override
  public void enableTrade(CreateTradeEventParamParam createTradeEventParam) {

  }

  @Override
  public void beforeHandleTrade(CreateTradeEventParamParam createTradeEventParam) {
//    super.beforeHandleTrade(createTradeEventParam);
//    EventEntity eventEntity = createTradeEventParam.getEvent();
//    Long orderQty = eventEntity.getQuantity();
//    // 1. 扣除可卖、可还数量
//    positionService.updateQty(new PositionUpdateQtyParam()
//        .setPositionId(eventEntity.getPositionId())
//        .setAvailableSaleQty(orderQty)
//        .setReturnableQty(orderQty)
//    );
//
//    BigDecimal frozenAmount = BigDecimal.valueOf(5);
//    // 2. 扣除可用资金
//    cashFlowService.saveWithIncremental(new CashFlowMessageDTO()
//        .setCustomerNo(eventEntity.getCustomerNo())
//        .setAmount(frozenAmount)
//        .setTotalAmount(frozenAmount.negate())
//    );
//
//    // 自动确认审批状态
//    TradeOrderEntity orderEntity = createTradeEventParam.getOrder();
//    orderEntity.setFlowStatus(FlowTypeEnum.CONFIRMED);
//    orderService.save(Collections.singletonList(orderEntity));
  }

  @Override
  public void handlingTradeExecReport(TradeExecReportParamParam tradeExecReportParam) {
//    EventEntity event = tradeExecReportParam.getEvent();
//    TradeExecReportDTO execReport = tradeExecReportParam.getExecReport();
//    Long quantity = execReport.getQuantity();
//
//    // 持仓更新
//    if(quantity > 0){
//      // 卖完后加当日卖出数量、持仓存续数量
//      positionService.updateQty(new PositionUpdateQtyParam()
//          .setPositionId(event.getPositionId())
//          .setIntradaySoldQty(-quantity)
//          .setLeaveShortQty(-quantity)
//      );
//    }
//
//    // 事件更新
//    event.setInitPrice(execReport.getExecPrice())
//        .setExistingQty(quantity)
//        .setTargetStatus(TargetStatusEnum.SUCCESS)
//    ;
//
//    if(quantity > 0L){
//      event.setContractStatus(ContractStatusEnum.LIVE);
//    }
//    eventService.save(Collections.singletonList(event));
  }

}
